package grape.workflow.rest.task.mvc;

import grape.common.exception.ExceptionTools;
import grape.common.rest.mvc.SuperController;
import grape.workflow.rest.task.form.TaskMoveRejectForm;
import grape.workflow.rest.task.vo.TaskRepresentationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.flowable.engine.RuntimeService;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务跳转
 */
@RestController
@RequestMapping("/workflow/task")
@Api(tags = "流程任务跳转相关接口")
public class TaskMoveController  extends SuperController {

    @Autowired
    private TaskMoveReverseHelper taskMoveReverseHelper;

    @Autowired
    private RuntimeService runtimeService;
    /**
     * 驳回任务
     * @param rejectForm 驳回任务表单
     * @return
     */
    @ApiOperation("驳回任务")
    @PreAuthorize("hasAuthority('process:tasks:reject')")
    @PostMapping(value = "/tasks/reject")
    @ResponseStatus(HttpStatus.OK)
    public Boolean taskReject(@RequestBody @Valid TaskMoveRejectForm rejectForm) {
        taskMoveReverseHelper.taskMoveByTaskId(rejectForm.getToDoTaskId(),rejectForm.getHistoryTaskId());
        return true;
    }

    /**
     * 撤回任务
     * @param historyTaskId 已办的任务id
     * @return
     */
    @ApiOperation("撤回任务")
    @PreAuthorize("hasAuthority('process:tasks:recall')")
    @PostMapping(value = "/tasks/{historyTaskId}/recall")
    @ResponseStatus(HttpStatus.OK)
    public Boolean taskRecall(@PathVariable String historyTaskId) {
        TaskMoveReverseHelper.NextRunningTasks nextRunningTasks = taskMoveReverseHelper.nextRunningTasks(historyTaskId);

        runtimeService.createChangeActivityStateBuilder().processInstanceId(nextRunningTasks.getHistoricTaskInstance().getProcessInstanceId())
                .moveActivityIdsToSingleActivityId(nextRunningTasks.getNextRunningTasks().stream().map(TaskInfo::getTaskDefinitionKey).collect(Collectors.toList()), nextRunningTasks.getHistoricTaskInstance().getTaskDefinitionKey()).changeState();

        return true;
    }
    /**
     * 可驳回的任务节点列表
     * @param toDoTaskId 待办任务id
     * @return
     */
    @ApiOperation("可驳回的任务节点列表")
    @PreAuthorize("hasAuthority('process:tasks:historyTasksThatCanReject:list')")
    @GetMapping(value = "/tasks/{toDoTaskId}/historyTasksThatCanReject/list")
    @ResponseStatus(HttpStatus.OK)
    public List<TaskRepresentationVo> historyTasksThatCanReject(@PathVariable String toDoTaskId) {
        List<HistoricTaskInstance> historicTaskInstances = taskMoveReverseHelper.historyTasksThatCanReject(toDoTaskId);
        if(isEmpty(historicTaskInstances)){
            throw ExceptionTools.dataNotExistRE("暂无可打回任务节点");
        }

        return historicTaskInstances.stream().map(TaskRepresentationVo::new).collect(Collectors.toList());
    }
}
